import {
  wxRequest
} from '../../../utils/server';
const _wxRequest = new wxRequest();
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    endDate: '',
    startDate: '',

    conditionTypeList: [],
    conditionTypeIndex: 0,
    conditionType: 1,

    activeTypeList: [],
    activeTypeIndex: 0,
    activeType: 1,

    isCondition: false,
    isActive: false,

    endTime: '',
    startTime: '',
  },

  // 使用条件类型
  pickerChangeCondition: function (e) {
    console.log(e);
    var index = e.detail.value;
    var conditionTypeList = this.data.conditionTypeList;
    var conditionType = ''
    conditionTypeList.map((item, i) => {
      if (i == index) {
        conditionType = item.key
      }
    })

    conditionType === 1 ? this.data.isCondition = false : this.data.isCondition = true

    this.setData({
      conditionTypeIndex: index,
      conditionType,
      isCondition: this.data.isCondition
    })
  },

  // 激活条件类型
  pickerChangeActive: function (e) {
    var index = e.detail.value;
    var activeTypeList = this.data.activeTypeList;
    var activeType = ''
    activeTypeList.map((item, i) => {
      if (i == index) {
        console.log(item.key);
        activeType = item.key
      }
    })

    activeType === 1 ? this.data.isActive = false : this.data.isActive = true

    this.setData({
      activeTypeIndex: index,
      activeType,
      isActive: this.data.isActive
    })
  },

  // 提交生成优惠券
  formSubmit: function (e) {
    var formdata = e.detail.value;
    console.log(formdata);

    formdata.startDate = formdata.startDate + ' ' + formdata.startTime + ':00'
    formdata.endDate = formdata.endDate + ' ' + formdata.endTime + ':00'
    delete formdata.endTime
    delete formdata.startTime

    var url = '/odr/protected/voucher/createVoucher';

    let params = {
      ...formdata,
      activeType: this.data.activeType,
      // useType: this.data.conditionType,
      supId: wx.getStorageSync('userInfo').orgId
    }

    console.log(params);

    let isComplete = false;

    for (const key in params) {
      if (params[key] === '') {
        wx.showToast({
          title: '请把信息填写完整!',
          icon: 'error'
        })
        return;
      } else {
        isComplete = true
      }
    }

    if (isComplete) {
      // 请求
      _wxRequest.wxTonkenAjax({
        url: app.globalData.apiUrl + url,
        method: 'POST',
        data: params,
      }, (res) => {
        wx.showToast({
          title: '生成优惠券成功!',
          icon: 'success'
        })
        setTimeout(() => {
          wx.redirectTo({
            url: '/pages/coupon_package/coupon_index',
          })
        }, 1500)
      })
    }
  },

  //过期时间
  bindDateChange: function (e) {
    console.log(e.detail.value);
    this.setData({
      endDate: e.detail.value
    })
  },

  bindTimeChange: function (e) {
    this.setData({
      endTime: e.detail.value
    })
  },

  // 开始时间
  bindStartDateChange: function (e) {
    this.setData({
      startDate: e.detail.value
    })
  },

  bindstartTimeChange: function (e) {
    this.setData({
      startTime: e.detail.value
    })
  },

  // 获取使用类型
  getUseType: function () {
    let url = '/sys/protected/comm/system/mall.VoucherEnum$UseType/enum/full'
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'GET'
    }, (res) => {
      console.log(res);
      this.data.conditionTypeList = res.list
      this.setData({
        conditionTypeList: this.data.conditionTypeList
      })
    })
  },

  // 获取激活类型
  getActiveType: function () {
    let url = '/sys/protected/comm/system/mall.VoucherEnum$ActiveType/enum/full'
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'GET'
    }, (res) => {
      console.log(res);
      this.data.activeTypeList = res.list
      this.setData({
        activeTypeList: this.data.activeTypeList
      })
    })
    
  },


  onLoad: function (options) {

    this.contentImgs = this.selectComponent("#contentImgs");
    this.contentVideo = this.selectComponent("#contentVideo");

    this.getUseType();
    this.getActiveType();

  },
  onShow: function () {

  },

})